package com.aiolos.weather.config;

import com.aiolos.weather.job.WeatherDataSyncJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Quartz配置类
 *
 * 配置Quartz需要2个Bean
 * JobDetail: 特定的一个Job
 * Trigger: 定义触发规则
 */
@Configuration
public class QuartzConfiguration {

    private static final int TIME = 1800; //更新频率: 半小时

    //JobDetail
    @Bean
    public JobDetail weatherDataSyncJobDetail(){
       //通过JobBuilder来创建JobDetail
       return JobBuilder.newJob(WeatherDataSyncJob.class)
                 .withIdentity("weatherDataSyncJob")
                 .storeDurably().build();
    }

    //Trigger
    @Bean
    public Trigger weatherDataSyncTrigger(){
        //定义定时规则：2s执行一次
        SimpleScheduleBuilder scheduleBuilder =
                SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(TIME).repeatForever();

        return TriggerBuilder.newTrigger()
                .forJob(weatherDataSyncJobDetail())
                .withIdentity("weatherDataSyncTrigger")
                .withSchedule(scheduleBuilder).build();
    }
}
